package org.luxor.common.netty.protocol.message;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import org.luxor.common.netty.constant.NettyReasonCode;
import org.luxor.common.netty.protocol.*;
import org.luxor.commons.core.web.IRestStatus;

/**
 * Netty PubComp Message
 * <p>
 * 服务端收到客户端(NettyPubRelMessage)收二次确认请求后的确认应答消息
 *
 * @author Mr.yan @date 2024/1/31
 */
public class NettyPubCompMessage extends NettyMessage implements ByteBufHolder {

    public NettyPubCompMessage(NettyFixedHeader fixedHeader, Object variableHeader, ByteBuf payload) {
        super(fixedHeader, variableHeader, payload);
    }

    @Override
    public NettyReasonCodeVariableHeader variableHeader() {
        return (NettyReasonCodeVariableHeader) super.variableHeader();
    }

    @Override
    public ByteBuf payload() {
        return content();
    }

    @Override
    public ByteBuf content() {
        return ByteBufUtil.ensureAccessible((ByteBuf) super.payload());
    }

    @Override
    public NettyPubCompMessage copy() {
        return replace(content().copy());
    }

    @Override
    public NettyPubCompMessage duplicate() {
        return replace(content().duplicate());
    }

    @Override
    public NettyPubCompMessage retainedDuplicate() {
        return replace(content().retainedDuplicate());
    }

    @Override
    public NettyPubCompMessage replace(ByteBuf content) {
        return new NettyPubCompMessage(fixedHeader(), variableHeader(), content);
    }

    @Override
    public int refCnt() {
        return content().refCnt();
    }

    @Override
    public NettyPubCompMessage retain() {
        content().retain();
        return this;
    }

    @Override
    public NettyPubCompMessage retain(int increment) {
        content().retain(increment);
        return this;
    }

    @Override
    public NettyPubCompMessage touch() {
        content().touch();
        return this;
    }

    @Override
    public NettyPubCompMessage touch(Object hint) {
        content().touch(hint);
        return this;
    }

    @Override
    public boolean release() {
        return content().release();
    }

    @Override
    public boolean release(int decrement) {
        return content().release(decrement);
    }

    public static NettyPubCompMessage from(String messageId) {
        return from(messageId, NettyReasonCode.SUCCESS, null);
    }

    public static NettyPubCompMessage from(String messageId, ByteBuf payload) {
        return from(messageId, NettyReasonCode.SUCCESS, payload);
    }

    public static NettyPubCompMessage from(String messageId, IRestStatus restStatus) {
        return from(messageId, restStatus, null);
    }

    public static NettyPubCompMessage from(String messageId, IRestStatus restStatus, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUB_COMP, false, NettyQoS.AT_MOST_ONCE);
        NettySendReplyMessageVariableHeader variableHeader = NettySendReplyMessageVariableHeader.from(messageId, restStatus);
        return new NettyPubCompMessage(fixedHeader, variableHeader, payload);
    }
}
